//
//  ZYCTeaLeaveApproveViewController.m
//  ZDSC
//
//  Created by 张威 on 2021/10/26.
//

#import "ZYCTeaLeaveApproveViewController.h"
#import "ZYCTeaLeaveRecordRootViewController.h"

@interface ZYCTeaLeaveApproveViewController ()<ZYCTeaLeaveApproveViewDelegate>

@property (nonatomic, strong) ZYCTeaLeaveApproveView *approveView;

@end

@implementation ZYCTeaLeaveApproveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (self.type == ZYCTeaLeaveApproveTypeRevoke) {
        self.title = @"撤销申请";
    }else{
        self.title = @"拒绝申请";
    }
    self.approveView.type = self.type;

    [self.view addSubview:self.approveView];
}

#pragma mark - 网络请求
// 撤销
- (void)revokeTeaLeave{
    NSDictionary *params = @{
        @"teacherLeaveId":self.teacherLeaveId,
        @"cancleReason":self.approveView.cancleReason
    };
    [MrjSVProgressHUD showWithStatus:@"撤销中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_CancleTask requestService:RequestServiceActiviti parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                [MrjToast makeToast:@"撤销失败" makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}
// 驳回

- (void)rejectTeaLeave{
    NSDictionary *params = @{
        @"businessKey":self.teacherLeaveId,
        @"taskId":self.taskId,
        @"status":@"0",
        @"toUsers":[self.toUsers componentsJoinedByString:@","],
        @"opinion":self.approveView.cancleReason
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_DoTask requestService:RequestServiceActiviti parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                [MrjToast makeToast:responseObject[@"body"] makeView:self.view];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    if (self.popBlock) {
                        self.popBlock();
                    }
                    [self.navigationController popViewControllerAnimated:YES];
                });
            }else{
                [MrjToast makeToast:@"加载失败" makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

#pragma mark - ZYCTeaLeaveApproveViewDelegate
- (void)teaLeaveApproveViewWithType:(ZYCTeaLeaveApproveType)type{
    if (type == ZYCTeaLeaveApproveTypeRevoke) {
        [self revokeTeaLeave];
    }else{
        [self rejectTeaLeave];
    }
}
#pragma mark - getters
- (ZYCTeaLeaveApproveView *)approveView {
    if (!_approveView) {
        _approveView = [[ZYCTeaLeaveApproveView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight)];
        _approveView.delegate = self;

    }
    return _approveView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
